'''
Created on Aug 21, 2010

@author: Administrator
'''
# -*- coding: utf-8 -*-
import sys
html5lib_path = "html5lib.zip"
sys.path.insert(0, html5lib_path)
import re
import urlparse
import logging
import datetime
import urllib

from google.appengine.api import urlfetch
from django.utils import simplejson as json
import html5lib
from html5lib import treebuilders
from xml.etree import cElementTree

_BDHeaders = {'User-Agent': 'Baiduspider', 'Referer': 'http://www.billboard.com',}
_FireFoxHeader = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.5.12', 'Referer': 'http://www.billboard.com',}

URL_NODE_TEMPLATE = "http://www.club.cn.sodexo.com/node/%d"
GEOCODE_URL_TEMPLATE = u"http://maps.google.com/maps/api/geocode/json?address=%s&sensor=false"

def _get_dom(url, header = _BDHeaders):
    try:
        fetch = urlfetch.fetch(url, headers = header)
        if fetch.status_code == 200:
            p = html5lib.HTMLParser(tree = treebuilders.getTreeBuilder('dom'))
            dom = p.parse(fetch.content)
            return dom
    except urlfetch.Error, e:
        logging.error('URL fetch has met exception: %s' % e.message)
        
    return None 

def _get_json(url, header = _BDHeaders):
    try:
        fetch = urlfetch.fetch(url, headers = header)
        if fetch.status_code == 200:
            return json.loads(fetch.content)
    except urlfetch.Error, e:
        logging.error('URL fetch has met exception: %s' % e.message)
        
    return None
def _get_text(node):
    rc = ''
    for node in node.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc.strip() 

def _get_url(id_sodexo):
    return URL_NODE_TEMPLATE%int(id_sodexo)

def parse_shop_by_id(id_sodexo):
    url = _get_url(id_sodexo)
    logging.info("start parse url:%s"%url)
    dom = _get_dom(url)
    name = None
    address = None
    phone = None
    tags = []
    related = []
    if dom is not None:
        id = "node-%s"%str(id_sodexo)
        h1s = dom.getElementsByTagName("h1")
        for h1 in h1s:
            attrvalue = h1.getAttribute("class")
            if attrvalue and attrvalue.lower().find("title") >= 0:
                name = _get_text(h1)
        divs = dom.getElementsByTagName("div")
        for div in divs:
            idvalue = div.getAttribute("class")
            if idvalue is not None:
                idvalue = idvalue.lower()
                if idvalue.find("outlet-address") >= 0:
                    logging.info("found address:")
                    logging.info("count:%d"%len(div.childNodes)) 
                    address = _get_text(div.childNodes[1])
                elif idvalue.find("outlet-phone") >= 0:
                    logging.info("found phone")
                    phone = _get_text(div.childNodes[1])
                elif idvalue.find("outlet-tags") >= 0:
                    logging.info("found tags")
                    lis = div.getElementsByTagName("li")
                    for li in lis:
                        tags.append(_get_text(li))
                elif idvalue.find("views-field-title") >= 0:
                    link = div.getElementsByTagName("a")[0]
                    if link is not None:
                        href = link.getAttribute("href")
                        logging.info("related:%s"%href)
                        items = href.split("/")
                        related.append(items[len(items)-1])
    return (name, address, phone, tags, related)

def geocode_address(address, id= None):
    para = "%E4%B8%8A%E6%B5%B7%E5%B8%82"+urllib.quote(address.encode("utf-8"))
    logging.info("para:%s"%para)
    url =  GEOCODE_URL_TEMPLATE % para
    logging.info("url:%s"%url)
    jo = _get_json(url)
    lat = None
    lng = None
    if jo is not None:
        logging.info("status: %s"%jo["status"]) 
        if jo["status"] == "OK":
            location = jo["results"][0]
            geometry = location["geometry"]
            if geometry:
                lat = float(geometry["location"]["lat"])
                lng = float(geometry["location"]["lng"])
        else:
            logging.error("Geocode address has met error")
            if id is not None:
                logging.info("node id:%s"%str(id))
            logging.info("request address:%s"%address)
            logging.info("request url:%s"%url)
            logging.info("response status:%s"%jo["status"])
    return (lat, lng)
                
            
        
    